#coding=utf8
import urllib2, re, urlparse
import cmrhelper

''' pictures parser '''
''' format: var pic_path = 'http://comic.niwawa.net/Pic/azimhdw/Vol_01/';
  datas[1] = '001.jpg';'''

picpathre = re.compile(r'var pic_path = \'(.*?)\';')
pagere = re.compile(r'(datas\[\d+?\] = \'.*?\');')

def getpiclist(url,title='unamed',mylog=None):
    "parser html source and js codes to generate piclist"
    content = urllib2.urlopen(url).read()
    # ugly code...
    datas = ['']*500
    pic_path = ''
    piclist = []
    try:
        if content:
            pic_path = picpathre.findall(content)[0]
            for item in pagere.findall(content):
                exec(item)
            for item in datas:
                if len(item) > 0:
                    turl = urlparse.urljoin(pic_path,item)
                    piclist.append([turl,turl,title])
        return piclist
    except  Exception, e:
        mylog.writeLog(str(e),'error')
